//
// Created by edward on 22-11-22.
//

#ifndef CONCURRENCE_SINGLETON_V0_H
#define CONCURRENCE_SINGLETON_V0_H

#include <mutex>

namespace edward {

    template<typename T>
    class Singleton {
        static T* p;
        static std::mutex mtx;
        template<typename ...Args>
        static T* getInstance(Args&& ...args) {
            if (!p) {
                std::lock_guard lk(mtx);
                if (!p) {
                    p = new T{std::forward<Args>(args)...};
                }
            }
            return p;
        }
    };

}
#endif //CONCURRENCE_SINGLETON_V0_H
